﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControl_ControlIndexInmast : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
                if (ViewState["flag"] == null)
                {
                    ViewState["flag"] = false;
                }
           
            LoadGridSearch();
        }
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        txtMaSo.Text = "0";
        txtName.Text = "";
        ViewState["flag"] = true;
        txtName.Focus();
        LoadGridInmast(0);
    }
    void LoadGridInmast(int orderSetID)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "ls_getOrderSetDetail", CommandType.StoredProcedure,
            new SqlParameter("@orderSetID", orderSetID));

        ViewState["inmast"] = db;

        grvInmast.DataSource = ViewState["inmast"];
        grvInmast.DataBind();
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        string ordersetid = "";
        string returnKq="";
        try
        {
            if ((bool)ViewState["flag"])
            {
                //insert
                ordersetid = dt.GetDataDepend(str, "[ls_saveOderSetMaster]", CommandType.StoredProcedure, ref error,
                    new SqlParameter("@orderSetID", 0),
                    new SqlParameter("@orderSetName", txtName.Text));
                ViewState["flag"] = false;
                returnKq = "Nghiệp vụ khai báo bộ thứ tự mới thành công";
            }
            else
            {
                //update
                ordersetid = dt.GetDataDepend(str, "[ls_saveOderSetMaster]", CommandType.StoredProcedure, ref error,
                  new SqlParameter("@orderSetID", int.Parse(txtMaSo.Text)),
                  new SqlParameter("@orderSetName", txtName.Text));
                returnKq = "Nghiệp vụ cập nhật bộ thứ tự thành công";
            }
            DataTable db = new DataTable();
            db = (DataTable)ViewState["inmast"];

            foreach (DataRow dr in db.Rows)
            {
                dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_saveOrderSetDetail]", ref error,
                    new SqlParameter("@orderSetID", int.Parse(ordersetid)),
                    new SqlParameter("@ItemKey", dr[1].ToString()),
                    new SqlParameter("@sortVal", int.Parse(dr[3].ToString())));

            }
            uscMsgBox1.AddMessage(returnKq, uscMsgBox.enmMessageType.Success);
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }        
    }
    protected void imgBtnDelete_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[ls_deleteOrderSet]", ref error,
                new SqlParameter("@orderSetID", int.Parse(txtMaSo.Text)));
            grvInmast.DataSource = null;
            grvInmast.DataBind();
            if (ViewState["inmast"] != null)
            {
                ViewState["inmast"] = null;
            }
            uscMsgBox1.AddMessage("Nghiệp vụ xóa bộ thứ tự mã hàng thành công", uscMsgBox.enmMessageType.Success);
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }     
    }
    protected void imgBtnFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    protected void grvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
          dt = new ClassExecute.ClassExecute();
          try
          {
              if (e.CommandName == "chon")
              {

                  GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
                  Label lblMaBoThuTu = (Label)grv.FindControl("lblMaBoThuTu");
                  Label lblMoTa = (Label)grv.FindControl("lblMoTa");

                  txtMaSo.Text = lblMaBoThuTu.Text;
                  txtName.Text = lblMoTa.Text;

                  LoadGridInmast(int.Parse(lblMaBoThuTu.Text));

                  grvSearch.DataSource = null;
                  grvSearch.DataBind();

                  ViewState["flag"] = false;
                  TabContainer1.ActiveTabIndex = 0;
              }
          }
          catch (Exception ex)
          {

              string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
              dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
          }   
    }
    protected void btSearch_Click(object sender, EventArgs e)
    {

    }
    protected void grvInmast_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
         dt = new ClassExecute.ClassExecute();
         try
         {
             string keys = grvInmast.DataKeys[e.RowIndex].Value.ToString();
             TextBox txtChiSoGrid = (TextBox)grvInmast.Rows[e.RowIndex].FindControl("txtChiSo");
             if ((txtChiSoGrid.Text == "") || (txtChiSoGrid.Text == "0"))
             {
                 txtChiSoGrid.Text = "1";
             }
             DataTable table = new DataTable();
             table = (DataTable)ViewState["inmast"];

             foreach (DataRow dr in table.Rows)
             {
                 if (dr[0].ToString() == keys)
                 {
                    
                     dr[2] = txtChiSoGrid.Text;
                     table.AcceptChanges();
                     ViewState["inmast"] = table;

                     grvInmast.EditIndex = -1;
                     grvInmast.DataSource = ViewState["inmast"];
                     grvInmast.DataBind();
                 }
               
             }
         }
         catch (Exception ex)
         {

             string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
             dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
         }    

    }
    protected void grvInmast_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        
    }
    protected void grvInmast_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvInmast.EditIndex = -1;
        grvInmast.DataSource = ViewState["inmast"];
        grvInmast.DataBind();
    }
    protected void grvInmast_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvInmast.EditIndex = e.NewEditIndex;
        grvInmast.DataSource = ViewState["inmast"];
        grvInmast.DataBind();
    }
    void LoadGridSearch()
    {
        dt = new ClassExecute.ClassExecute();
        try
        {
            DataTable db = dt.Get_Data_DataTable(str, ref error, "[ls_getOrderSetList]", CommandType.StoredProcedure);
            grvSearch.DataSource = db;
            grvSearch.DataBind();
        }
        catch (Exception ex)
        {

            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        }   
    }

}